package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.ShoppingCart;
import com.itheima.reggie.sercice.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R saveShopping(@RequestBody ShoppingCart shoppingCart) {
        Long userId = BaseContext.getId();
         /* 菜品或者套餐是否点过
     如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
     如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
     如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
     */
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        //判断用户当前点的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) { //dishId不为空 就代表点的是菜
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {  //否则就是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //获取购物车内数据
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (one == null) {
            //如果购物车是空   就是添加
            //设置上数量 和购买人id
            shoppingCart.setUserId(userId);
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        } else {
            //数量不是1 就是修改   把数量加1
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }

        return R.success(shoppingCart);
    }

    //接口二：查询购物车
    @GetMapping("/list")
    public R list() {
        Long id = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, id);

        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);

    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        //用户id
        Long userId = BaseContext.getId();
        //菜品id
        Long dishId = shoppingCart.getDishId();
        //套餐id
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //判断用户
        wrapper.eq(ShoppingCart::getUserId, userId);
        if (dishId != null) { //是菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {  //套餐
            wrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        //获取购物车中的一个的id
        ShoppingCart oneId = shoppingCartService.getOne(wrapper);
        //菜的数量
        Integer number = oneId.getNumber();
        // number -1 然后操作：
        number--;
        //操作之后再保存数量
        oneId.setNumber(number);
        if (number > 0) { //菜的数量大于0  就是修改操作
            shoppingCartService.updateById(oneId);
        } else {  //否则就是删除
            shoppingCartService.remove(wrapper);
        }

        return R.success(oneId);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R clean() {
        //获取userId
        Long id = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, id);
        shoppingCartService.remove(wrapper);

        return R.success("清空购物车成功");
    }
}
